home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / NetWarmer / source / Dragging.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-11  |  6.1 KB  |  309 lines  |  [TEXT/KAHL]

  1. /* © 1988, Bowers Development Corp. */
  2. /* Dragging.c */
  3.  
  4. #include "Globals.h"
  5. #include "ResourceDefs.h"    
  6.  
  7. #include "Dragging.h"
  8.  
  9.  
  10. /*----------*/
  11. short Abs (short    x);
  12. short Abs (x)
  13. short            x;
  14. {
  15.     return ((x < 0) ? (-x) : (x));
  16. } /*Abs*/
  17.  
  18. /*----------*/
  19. Boolean StartMove  (startPt, constraint)
  20. Point        *startPt;
  21. short        *constraint;
  22. {
  23.     #define    slop    2
  24.  
  25.     Rect            slopRect;
  26.     Point            mousePt;
  27.  
  28.     #define sP        (*startPt)
  29.     SetRect (&slopRect, sP.h, sP.v, sP.h + 1, sP.v + 1);
  30.     #undef sP
  31.  
  32.     InsetRect (&slopRect, -slop, -slop);
  33.  
  34.     mousePt = *startPt;
  35.     while (StillDown () && PtInRect (mousePt, &slopRect)) {
  36.         GetMouse (&mousePt);
  37.     }
  38.  
  39.     if (StillDown ()) {
  40.         *constraint = noConstraint;
  41.         if ((curEvent.modifiers & optionKey) != 0) {
  42.             if (Abs (mousePt.h - (*startPt).h)
  43.             >  Abs (mousePt.v - (*startPt).v)) {
  44.                 *constraint = hAxisOnly;
  45.             } else {
  46.                 *constraint = vAxisOnly;
  47.             }
  48.         }
  49.     }
  50.     
  51.     *startPt = mousePt;
  52.     if ((*startPt).h > (slopRect).right)  { (*startPt).h = (slopRect).right;    }
  53.     if ((*startPt).h < (slopRect).left)   { (*startPt).h = (slopRect).left - 1;    }
  54.     if ((*startPt).v > (slopRect).bottom) { (*startPt).v = (slopRect).bottom;    }
  55.     if ((*startPt).v < (slopRect).top)    { (*startPt).v = (slopRect).top - 1;    }
  56.     
  57.     return (StillDown ());
  58. } /*StartMove*/
  59.  
  60. /*----------*/
  61. Boolean TrackMove  (dragRgn,
  62.                     startPt,
  63.                     bounds,
  64.                     constraint,
  65.                     deltaH,
  66.                     deltaV,
  67.                     actionProc)
  68. RgnHandle        dragRgn;
  69. Point            startPt;
  70. Rect            bounds;
  71. short            constraint;
  72. short            *deltaH;
  73. short            *deltaV;
  74. DragProcPtr        actionProc;
  75. {
  76.     #undef slop
  77.     #define slop    20
  78.     
  79.     Rect            limitRect;
  80.     Rect            slopRect;
  81.     long            delta;
  82.     register Rect        *dragBBox;
  83.     
  84.     dragBBox = &(**dragRgn).rgnBBox;
  85.     SetRect (&limitRect,
  86.                 bounds.left   - dragBBox->left,
  87.                 bounds.top    - dragBBox->top,
  88.                 bounds.right  - dragBBox->right + 1,
  89.                 bounds.bottom - dragBBox->bottom + 1);
  90.     
  91.     OffsetRect (&limitRect, startPt.h, startPt.h);
  92.     slopRect = limitRect;
  93.     InsetRect (&slopRect, -slop, -slop);
  94.     
  95.     delta = DragGrayRgn (dragRgn, startPt, &limitRect,
  96.                             &slopRect, constraint, (ProcPtr) actionProc);
  97.     *deltaV = HiWord (delta);
  98.     *deltaH = LoWord (delta);
  99.  
  100.     if (( delta  == 0)
  101.     ||  (*deltaH == 0x8000)
  102.     ||  (*deltaV == 0x8000)) {
  103.         return (false);
  104.     } else {
  105.         return (true);
  106.     }
  107. } /*TrackMove*/
  108.  
  109. /*----------*/
  110. Boolean TrackRange (anchorPt, range)
  111. Point        anchorPt;
  112. Rect        *range;
  113. {
  114.     PenState        savePen;
  115.     Point            curPos;
  116.     Point            newPos;
  117.     long            lastTicks;
  118.  
  119.     GetPenState (&savePen);
  120.     PenNormal ();
  121.     PenMode (notPatXor);
  122.     PenPat (gray);
  123.  
  124.     newPos = anchorPt;
  125.     curPos = newPos;
  126.     Pt2Rect (anchorPt, curPos, range);
  127.     FrameRect (range);    /*Draw*/
  128.  
  129.     lastTicks = 0;    
  130.     while (StillDown ()) {
  131.         if (TickCount () >= lastTicks + 2) {
  132.             GetMouse (&newPos);
  133.             if (!EqualPt (newPos, curPos)) {
  134.                 FrameRect (range);    /*Undraw*/
  135.                 curPos = newPos;
  136.                 Pt2Rect (anchorPt, curPos, range);
  137.                 FrameRect (range);    /*Draw*/
  138.             }
  139.             lastTicks = TickCount ();
  140.         }
  141.     } /*while*/
  142.     
  143.     FrameRect (range);    /*Undraw*/
  144.     
  145.     SetPenState (&savePen);
  146.  
  147.     return (!EmptyRect (range));
  148. } /*TrackRange*/
  149.  
  150. /*----------*/
  151. void PinPt (Rect        limitRect,
  152.             Point        *where);
  153. void PinPt (limitRect, where)
  154. Rect            limitRect;
  155. Point            *where;
  156. {
  157.     #define lR    limitRect
  158.     if (where->v < lR.top)        where->v = lR.top;
  159.     if (where->h < lR.left)        where->h = lR.left;
  160.     if (where->v > lR.bottom)    where->v = lR.bottom;
  161.     if (where->h > lR.right)    where->h = lR.right;
  162.     #undef LR
  163. } /*PinPt*/
  164.  
  165. /*----------*/
  166. Boolean TrackRect  (sizeRect, limitRect, actionProc)
  167. Rect            *sizeRect;
  168. Rect            limitRect;
  169. DragProcPtr        actionProc;
  170. {
  171.     PenState        savePen;
  172.     Point            curPos;
  173.     Point            newPos;
  174.     Point            offset;
  175.     Rect            slopRect;
  176.     long            lastTicks;
  177.     long            temp;
  178.     
  179.     #define InSlop        PtInRect (botRight (sizeRect), &slopRect)
  180.  
  181.     GetPenState (&savePen);
  182.     PenNormal ();
  183.     PenMode (notPatXor);
  184.     PenPat (gray);
  185.  
  186.     slopRect = limitRect;
  187.     InsetRect (&slopRect, -20, -20);
  188.     
  189.     GetMouse (&curPos);
  190.     temp = DeltaPoint (botRight (sizeRect), curPos);
  191.     offset = *(Point *) &temp;
  192.  
  193.     FrameRect (sizeRect);    /*Draw*/
  194.  
  195.     lastTicks = 0;    
  196.     while (StillDown ()) {
  197.         if (TickCount () >= lastTicks + 2) {
  198.             GetMouse (&newPos);
  199.             if (!EqualPt (newPos, curPos)) {
  200.                 if (InSlop) {
  201.                     FrameRect (sizeRect);    /*Undraw*/
  202.                 }
  203.                 curPos = newPos;
  204.                 AddPt (offset, &newPos);
  205.                 botRight (sizeRect) = newPos;
  206.                 if (InSlop) {
  207.                     PinPt (limitRect, &botRight (sizeRect)); 
  208.                     FrameRect (sizeRect);    /*Draw*/
  209.                 }
  210.                 if (actionProc != nil) {
  211.                     CallPascal (actionProc);
  212.                 }
  213.             }
  214.             lastTicks = TickCount ();
  215.         }
  216.     } /*while*/
  217.  
  218.     if (InSlop) {
  219.         FrameRect (sizeRect);    /*Undraw*/
  220.     }
  221.     
  222.     SetPenState (&savePen);
  223.     
  224.     return (InSlop);
  225.     
  226.     #undef InSlop
  227. } /*TrackRect*/
  228.  
  229. /*----------*/
  230. void FrameLine (Rect        sizeRect);
  231. void FrameLine (sizeRect)
  232. Rect            sizeRect;
  233. {
  234.     MoveTo (sizeRect.left, sizeRect.top);
  235.     LineTo (sizeRect.right - 1, sizeRect.bottom - 1);
  236. } /*FrameLine*/
  237.  
  238. /*----------*/
  239. Boolean TrackLine  (sizeRect, limitRect, actionProc)
  240. Rect            *sizeRect;
  241. Rect            limitRect;
  242. DragProcPtr        actionProc;
  243. {
  244.     PenState        savePen;
  245.     Point            curPos;
  246.     Point            newPos;
  247.     Point            Offset;
  248.     Rect            slopRect;
  249.     long            lastTicks;
  250.     long            temp;
  251.     
  252.     #define InSlop        PtInRect (botRight (sizeRect), &slopRect)
  253.  
  254.     GetPenState (&savePen);
  255.     PenNormal ();
  256.     PenMode (notPatXor);
  257.     PenPat (gray);
  258.  
  259.     slopRect = limitRect;
  260.     InsetRect (&slopRect, -20, -20);
  261.     
  262.     GetMouse (&curPos);
  263.     temp = DeltaPoint (botRight (sizeRect), curPos);
  264.     Offset = *(Point *) &temp;
  265.  
  266.     FrameLine (*sizeRect);    /*Draw*/
  267.  
  268.     lastTicks = 0;    
  269.     while (StillDown ()) {
  270.         if (TickCount () >= lastTicks + 2) {
  271.             GetMouse (&newPos);
  272.             if (!EqualPt (newPos, curPos)) {
  273.                 if (InSlop) {
  274.                     FrameLine (*sizeRect);    /*Undraw*/
  275.                 }
  276.                 curPos = newPos;
  277.                 AddPt (Offset, &newPos);
  278.                 botRight (sizeRect) = newPos;
  279.                 /* ?? constrain line to be horizontal or vertical ?? */
  280.                 /*    With sizeRect do {
  281.                 /*        if ((right - left) > (bottom - top)) {
  282.                 /*            bottom = top + 1;
  283.                 /*        } else {
  284.                 /*            right = left + 1;
  285.                 /*        }
  286.                 /*    } /*with*/
  287.                 if (InSlop) {
  288.                     PinPt (limitRect, &botRight (sizeRect)); 
  289.                     FrameLine (*sizeRect);    /*Draw*/
  290.                 }
  291.                 if (actionProc != nil) {
  292.                     CallPascal (actionProc);
  293.                 }
  294.             }
  295.             lastTicks = TickCount ();
  296.         }
  297.     } /*while*/
  298.  
  299.     if (InSlop) {
  300.         FrameLine (*sizeRect);    /*Undraw*/
  301.     }
  302.     
  303.     SetPenState (&savePen);
  304.     
  305.     return (InSlop);
  306.     
  307.     #undef InSlop
  308. } /*TrackLine*/
  309.